package com.eptok.gw.szcups.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author haichen.ma
 * 
 */
public class DateUtil {
	/**
	 * DateUtils
	 */
	private DateUtil() {
	}

	/**
	 * 获取当前日期, 默认格式为yyyy-MM-dd
	 * 
	 * @return String
	 */
	public static String getDate() {
		return DateUtil.formatDate("yyyy-MM-dd");
	}

	/**
	 * getDate
	 * 
	 * @param pattern
	 *            String
	 * @return String
	 */
	public static Date formatDate(String date, String pattern) throws Exception{
		return new SimpleDateFormat(pattern).parse(date);
	}
	
	/**
	 * getDate
	 * 
	 * @param pattern
	 *            String
	 * @return String
	 */
	public static String formatDate(String pattern) {
		return new SimpleDateFormat(pattern).format(new Date());
	}

	/**
	 * getDate
	 * 
	 * @param date
	 * @return Date
	 * @author haichen.ma
	 * @since 2006-12-28
	 */
	public static Date getDate(String date) {
		DateFormat format = null;
		
		if (date != null) {
			if (date.length() == 8) {
				format = new SimpleDateFormat("yyyyMMdd");
			}
			else if (date.length() == 10) {
				format = new SimpleDateFormat("yyyy-MM-dd");
			}
			else {
				format = new SimpleDateFormat("yyyy-MM-dd");
			}
		}
		
		try {
			return format.parse(date);
		} catch (ParseException ex) {
			ex.printStackTrace();
		}

		return null;
	}
	
	
	/**
	 * @param date
	 * @return
	 */
	public static String getDate(Date date) {
		return DateUtil.getDate(date, "yyyyMMdd");
	}

	/**
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String getDate(Date date, String pattern) {
		return new SimpleDateFormat(pattern).format(date);
	}

	/**
	 * incDay
	 * 
	 * @param date
	 *            Date
	 * @return Date
	 */
	public static Date incDay(Date date, int dayNumber) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, dayNumber);

		return calendar.getTime();
	}

	/**
	 * decDay
	 * 
	 * @param date
	 *            Date
	 * @return Date
	 */
	public static Date decDay(Date date, int dayNumber) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, -dayNumber);

		return calendar.getTime();
	}
	
	/**
	 * 后推月份.
	 * 
	 * @param date
	 * @param monthNumber
	 * @return
	 * 
	 * @author haichen.ma
	 * @date 2008-02-22
	 */
	public static Date incMonth(Date date, int monthNumber) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, monthNumber);
		
		return calendar.getTime();
	}
	
	/**
	 * 前推月份.
	 * 
	 * @param date
	 * @param monthNumber
	 * @return
	 * 
	 * @author haichen.ma
	 * @date 2008-02-22
	 */
	public static Date decMonth(Date date, int monthNumber) {
		return incMonth(date, -monthNumber);
	}

	/**
	 * getDateDiffDays
	 * 
	 * @param curDate
	 * @param oriDate
	 * @return int
	 * @author haichen.ma
	 * @since 2006-12-28
	 */
	public static int getDateDiffDays(Date curDate, Date oriDate) {
		final int MS_PER_DAY = 1000 * 60 * 60 * 24;
		Date startDate = new Date();
		Date endDate = new Date();

		try {
			startDate = new java.text.SimpleDateFormat("yyyy-MM-dd")
					.parse(getDate(curDate,"yyyy-MM-dd"));
			endDate = new java.text.SimpleDateFormat("yyyy-MM-dd")
					.parse(getDate(oriDate,"yyyy-MM-dd"));
		} catch (ParseException ex) {
			ex.printStackTrace();
		}
//		Math.abs
		return (int) ((startDate.getTime() - endDate.getTime())
				/ MS_PER_DAY);
	}
	
	/**
	 * clearTimeOfDay
	 * 
	 * @param date
	 * @return Date
	 * @author haichen.ma
	 * @since 2006-12-28
	 */
	public static Date clearTimeOfDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);

		return calendar.getTime();
	}
	
	/**
	 * 将字符串格式返回默认的日期格式yyyy-MM-dd(不带时间)
	 * @param date 为空则返回null
	 * @return
	 * @author heng.li
	 * @since 2008-9-24
	 */
	public static Date strToDefaultDate(String date) {
		if (date == null || "".equals(date)) {
			return null;
		}
		try {
			return clearTimeOfDay(new SimpleDateFormat("yyyy-MM-dd").parse(date));
		} catch (ParseException ex) {
			ex.printStackTrace();
		}
		return null;
	}

	
}
